package com.lsl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lsl.gulimall.product.service.AttrService;
import com.lsl.gulimall.product.vo.request.AttrGroupRelationInVo;

import com.lsl.gulimall.product.vo.request.AttrGroupWithAttrsInVo;
import com.lsl.gulimall.product.vo.response.AttrGroupWithAttrsOutVo;
import com.lsl.gulimall.product.vo.response.AttrOutVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lsl.gulimall.product.entity.AttrGroupEntity;
import com.lsl.gulimall.product.service.AttrGroupService;
import com.lsl.common.utils.PageUtils;
import com.lsl.common.utils.R;



/**
 * 属性分组
 *
 * @author liushengli
 * @email sunlightcs@gmail.com
 * @date 2021-03-14 17:27:58
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("categoryId") long categoryId ){
        PageUtils page = attrGroupService.queryPage(params,categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getPathById(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 获取属性分组没有关联的其他属性
     * @param params
     * @param groupId
     * @return
     */
   @GetMapping("/{attrgroupId}/noattr/relation")
   public  R getattrRelation(@RequestParam Map<String,Object> params,@PathVariable("attrgroupId")Long groupId ){
       PageUtils page =attrService.getNoattrRelation(params,groupId);
        return R.ok().put("page",page);
   }

    /**
     * 获取属性分组的关联的所有属性
     *
     * @param groupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public  R getNoattrRelation(@PathVariable("attrgroupId")Long groupId ){
        List<AttrOutVo> attrOutVos = attrService.getattrRelation(groupId);
        return R.ok().put("data",attrOutVos);
    }
    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public  R getWithAttr(@PathVariable("catelogId")Long catelogId ){
        List<AttrGroupWithAttrsOutVo> attrOutVos = attrService.getWithAttrByCateLogId(catelogId);
        return R.ok().put("data",attrOutVos);
    }

    /**
     * 保存关联分组
     */
    @PostMapping("/attr/relation")
    //@RequiresPermissions("product:attrgroup:save")
    public R addRelation(@RequestBody List<AttrGroupRelationInVo>attrGroupRelationInVos){
		attrGroupService.saveAttrGroupRelation(attrGroupRelationInVos);
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:update")
    public R save(@RequestBody AttrGroupWithAttrsInVo attrGroupWithAttrsInVo){
        AttrGroupEntity attrGroupEntity = new AttrGroupEntity();
        BeanUtils.copyProperties(attrGroupWithAttrsInVo,attrGroupEntity);
        attrGroupService.save(attrGroupEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除分组
     */
    @PostMapping("delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R deleteAttrGroup(@RequestBody List<Long>ids){
        attrGroupService.removeByIds(ids);
        return R.ok();
    }

    /**
     * 删除关联信息
     */
    @PostMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationInVo>attrGroupRelationInVos){
		attrGroupService.deleteRelation(attrGroupRelationInVos);
        return R.ok();
    }
}
